<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="关键字/id/试卷标题" style="width: 200px;" class="filter-item"
        @keyup.enter.native="handleFilter" />

      <el-select v-model="listQuery.address_id" placeholder="地区选择" clearable style="width: 150px" class="filter-item">
        <el-option v-for="(item, index) in address" :key="index" :label="item.name" :value="item.id" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="library_add">添加新试卷</el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="ID" prop="id" sortable="custom" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="试卷名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="试卷封面" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.image">
            <img width="100px" :src="qiniuDomain + scope.row.image"></img>
          </span>
          <span v-else>
            无
          </span>
        </template>
      </el-table-column>
      <el-table-column label="所属地区" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加日期" align="center">
        <template slot-scope="{ row }">
          {{ _date(row.create_time) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <div>
            <span v-permission="['/library/libraryEdit']">
              <el-button size="small" type="primary" @click="library_edit(scope.row)">修改</el-button>
              <el-button size="small" type="primary" @click="library_del(scope.row)">删 除</el-button>
              <el-button size="small" type="primary" @click="topic_list(scope.row)">题 目</el-button>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="library_title" :visible.sync="library_edit_show">
      <el-form>
        <el-form-item label="试卷名称" label-width="150px">
          <el-input v-model="item.name" />
        </el-form-item>
        <el-form-item label="试卷所属地区" label-width="150px">
          <el-select v-model="item.address_id" placeholder="分类" clearable style="width: 150px" class="filter-item">
            <el-option v-for="(item, index) in address" :key="index" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="试卷封面" label-width="150px">
          <imageUpload v-model="item.image"> </imageUpload>
        </el-form-item>
        <div v-for="(v,k) in item.material" :key="k">
          <el-form-item @click="materialDel" :label="'试卷材料' + (k+1)" label-width="150px">
            <el-input type="textarea" :rows="4" v-model="v.content" />
            <span style="color: #0000F0; float: right; cursor:pointer;" @click="materialDel(v)">删除本条</span>
          </el-form-item>
        </div>
        <span style="color: green; font-size: 17px; float: right; cursor:pointer;" @click="materialAdd()">新增一条材料</span>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="library_handle(item)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    libraryList,
    libraryEdit,
    libraryDel
  } from '@/api/library';
  import {
    addressList
  } from '@/api/config';
  import waves from '@/directive/waves';
  import Pagination from '@/components/Pagination';
  import permission from '@/directive/permission/index.js';
  import {
    dateHandle
  } from '@/api/dateHandle';
  import imageUpload from '@/components/Upload/image';


  export default {
    components: {
      Pagination,
      imageUpload
    },
    directives: {
      waves,
      permission,

    },
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
        },
        address: {},
        item: {},
        library_edit_show: false,
        library_title: '添加试卷',

        list1: [{
            user_id: 1,
            number: 1,
            price: 10
          }, {
            user_id: 2,
            number: 3,
            price: 20
          },
          {
            user_id: 2,
            number: 3,
            price: 30
          },
        ],
        list2: [],
        list3: []
      };
    },
    created() {
      this.list1.forEach(res => {
        this.list2[res.user_id] = []
        this.list3[res.user_id] = {
          number: 0,
          price: 0,
          yunfei: 0
        }
      })

      this.list1.forEach((res, k) => {
        if (this.list2[res.user_id]) {
          this.list2[res.user_id].push(res)
          this.list3[res.user_id].number += res.number //计算出该商家的总书本数量
          this.list3[res.user_id].price += res.price * res.number //计算出该商家的总书本费用
        }
      })
      this.list3.forEach((res, k) => {
        let yunfei = 0
        if (res.price >= 100) {
          //如果总价超过100则该商家免邮
          yunfei = 0
        } else if (res.number < 5) {
          //如果购买的书本数量少于5则运费=1元
          yunfei = 1
        } else {
          //如果购买的书本数量>=5,则运费的10
          yunfei = 10
        }
        this.list3[k].yunfei = yunfei
      })
      console.log(this.list2);
      console.log(this.list3);

      this.getList()
      this.getAddress()

    },
    methods: {
      _date(t) {
        return dateHandle('Y-m-d H:i:s', t);
      },
      topic_list(item) {
        this.$router.push({
          name: 'topicList',
          params: {
            id: item.id,
            name: item.name
          }
        })
      },
      library_del(item) {
        this.$confirm('确认删除该试卷?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          libraryDel({
            id: item.id
          }).then(res => {
            if (res.code == 20000) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.show = false;
              this.getList()
            }
          })
        }).catch(() => {})
      },
      library_handle(data) {
        libraryEdit(this.item).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: res.msg
            });
            this.getList()
            this.listLoading = false
            this.library_edit_show = false
          }
        })
      },
      materialDel(k) {
        this.item.material.splice(k, 1)
      },
      materialAdd() {
        this.item.material.push({
          content: ''
        })
      },
      getAddress() {
        addressList().then(res => {
          this.address = res.data
        })
      },
      library_add() {
        this.item = {
          material: []
        }
        this.library_edit_show = true
        this.library_title = '添加试卷'
      },
      library_edit(item) {
        this.item = item
        this.library_edit_show = true
        this.library_title = '编辑试卷'
      },
      getList() {
        this.listLoading = true;
        libraryList(this.listQuery).then(res => {
          this.list = res.data.data;
          this.total = res.data.total;
          setTimeout(() => {
            this.listLoading = false;
          }, 0.3 * 1000);
        });
      },

    }
  };
</script>
